#include "channel.hpp"
#include "event_loop.hpp"
#include "event_error.hpp"
#include <error.h>

using namespace event_framework;

void Channel::HandleClose()
{
    if (revents & EPOLLHUP && !(revents & EPOLLIN))
    {
        DisableAll();
        Update();
        int localfd = fd;
        if (closedCb) closedCb();
    }
}
void Channel::HandleError()
{
    if (revents & (EPOLLERR))
    {
        DisableAll();
        Update();
        int localfd = fd;
        if (exceptionCb) exceptionCb(static_cast<int>(EVENT_ERR_SOCK_UNKOWN));     // TODO: errcode definition
    }
}
void Channel::HandleRead()
{
    if (revents &(EPOLLIN | EPOLLPRI | EPOLLRDHUP))
    {
        if (readableCb) readableCb();
    }
}
void Channel::HandleWrite()
{
    if (revents & EPOLLOUT)
    {
        if(writeableCb) writeableCb();
    }
}
void Channel::Update()
{
    loop.UpdateChannel(shared_from_this());
}